package m.jclecai.com.view.klpk;

import android.content.Context;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import m.jclecai.com.R;


/**
 * Created by mawenhao on 2017/6/7.
 *
 * 这个是poker彩的种类；这里的话，我们需要将其弄成一个复选框的样子；
 */

public class PokerCategory extends LinearLayout implements View.OnClickListener{

    public Status mCurStatus= Status.unchecked;
    private TextView mTitle;
    private TextView mSubTitle;
    private LinearLayout mLlBg;

    public PokerCategory(Context context) {
        this(context,null);
    }

    public PokerCategory(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PokerCategory(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.category_poker,this);

        mLlBg = ((LinearLayout) view.findViewById(R.id.ll_bg));
        mTitle = ((TextView) view.findViewById(R.id.tv_bottom_title));
        mSubTitle = ((TextView) view.findViewById(R.id.tv_sub_title));
        mLlBg.setBackgroundDrawable(getResources().getDrawable(R.drawable.category_poker_normal));
        mLlBg.setOnClickListener(this);
    }

    public void setTitle(String title){
        mTitle.setText(title);
    }

    public String getTitle(){
        return mTitle.getText().toString().trim();
    }

    public void setSubTitle(String subTitle){

        int startIndex=0;
        int endIndex=0;
        SpannableString spannableString = new SpannableString(subTitle);

        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(0xFFfec345);

        if (subTitle.indexOf("最高奖")!=-1) {
            //有最高奖
            startIndex=subTitle.indexOf("最高奖")+"最高奖".length();
        }else if(subTitle.indexOf("奖金")!=-1){
            //有奖金字段；
            startIndex=subTitle.indexOf("奖金")+"奖金".length();
        }
        endIndex=subTitle.indexOf("元");
        spannableString.setSpan(foregroundColorSpan,startIndex,endIndex, SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
        mSubTitle.setText(spannableString);
    }



    public OnCheckedChangeListener mOnCheckedChangeListener;

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }

    public View getView(){
        return mLlBg;
    }

    @Override
    public void onClick(View v) {
        changeStatus();
    }

    private void changeStatus() {
        //设置状态；
        if (mCurStatus.equals(Status.checked)) {

            setChecked(false);

            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.isChecked(this,false);
            }
        }else{
            setChecked(true);
            //这里的话，背景固定的；
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.isChecked(this,true);
            }
        }
    }

    public interface OnCheckedChangeListener {
        void isChecked(PokerCategory view, boolean isChecked);
    }

    public void setChecked(boolean isChecked){
        if (isChecked) {
            //表示勾选了；
            mCurStatus= Status.checked;
            mLlBg.setBackgroundDrawable(getResources().getDrawable(R.drawable.category_poker_selected));
        }else{
            mCurStatus= Status.unchecked;
            mLlBg.setBackgroundDrawable(getResources().getDrawable(R.drawable.category_poker_normal));
        }
    }

    public  enum  Status{
        checked,unchecked
    }
}
